package com.itheima.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Role;
import com.itheima.health.service.RoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Wang
 * @version 1.0.0
 * @createTime 2020年08月25日 10:04:00
 * @Description: Role  前端控制
 */
@RestController
@RequestMapping("/role")
@Slf4j
public class RoleController {

    @Reference
    private RoleService roleService;

    /**
     * 分页查询
     *
     * @param queryPageBean
     * @return
     */
    @GetMapping("/findPage")
    @PreAuthorize("hasAuthority('ROLE_QUERY')")
    public Result findByPage(QueryPageBean queryPageBean) {
        PageResult pageResult = roleService.findByPage(queryPageBean);
        return new Result(true, MessageConst.ACTION_SUCCESS, pageResult);
    }

    /**
     * 添加角色信息关联菜单和权限表
     *
     * @param role
     * @param
     * @param
     * @return
     */
    @PostMapping("/add")
    @PreAuthorize("hasAuthority('ROLE_ADD')")
    public Result add(@RequestParam(value = "menuIds") List<Integer> menuIds, @RequestParam(value = "permissionIds") List<Integer> permissionIds, @RequestBody Role role) {
        roleService.add(role, menuIds, permissionIds);
        return new Result(true, MessageConst.ACTION_SUCCESS);
    }

    /**
     * 根据id查询角色信息
     *
     * @param id
     * @return
     */
    @GetMapping("/findById")
    @PreAuthorize("hasAuthority('ROLE_QUERY')")
    public Result findById(Integer id) {
        Role role = roleService.findById(id);
        return new Result(true, MessageConst.ACTION_SUCCESS, role);
    }

    /**
     * 根据roleId 在t_role_menu中查找
     *
     * @param id
     * @return
     */
    @GetMapping("/findMenuIdsByRoleId")
    @PreAuthorize("hasAuthority('ROLE_QUERY')")
    public Result findMenuIdsByRoleId(Integer id) {
        List<Integer> menuIds = roleService.findMenuIdsByRoleId(id);
        return new Result(true, MessageConst.ACTION_SUCCESS, menuIds);
    }

    /**
     * 根据roleId 查询permissionIds
     *
     * @param id
     * @return
     */
    @GetMapping("/findPermissionIdsByRoleId")
    @PreAuthorize("hasAuthority('ROLE_QUERY')")
    public Result findPermissionIdsByRoleId(Integer id) {
        List<Integer> permissionIds = roleService.findPermissionIdsByRoleId(id);
        return new Result(true, MessageConst.ACTION_SUCCESS, permissionIds);
    }

    /**
     * 更新role表和关联表中的数据
     *
     * @param role
     * @param menuIds
     * @param permissionIds
     * @return
     */
    @PostMapping("/edit")
    @PreAuthorize("hasAuthority('ROLE_EDIT')")
    public Result edit(@RequestBody Role role, @RequestParam(value = "menuIds") List<Integer> menuIds, @RequestParam(value = "permissionIds") List<Integer> permissionIds) {
        roleService.edit(role, menuIds, permissionIds);
        return new Result(true, MessageConst.ACTION_SUCCESS);
    }

    /**
     * 根据id删除role信息
     * @param id
     * @return
     */
    @PostMapping("/delete")
    @PreAuthorize("hasAuthority('ROLE_DELETE')")
    public Result delete(Integer id) {
        roleService.delete(id);
        return new Result(true, MessageConst.ACTION_SUCCESS);
    }
}
